package cn.demon.pet.service.impl;

import cn.demon.basic.service.IBaiduAiAuditService;
import cn.demon.basic.util.BaiduAuditUtils;
import cn.demon.basic.util.LoginContext;
import cn.demon.org.domain.Employee;
import cn.demon.org.mapper.EmployeeMapper;
import cn.demon.pet.domain.Pet;
import cn.demon.pet.domain.PetDetail;
import cn.demon.pet.domain.PetOnlineAuditLog;
import cn.demon.pet.domain.PetType;
import cn.demon.pet.mapper.PetDetailMapper;
import cn.demon.pet.mapper.PetMapper;
import cn.demon.pet.mapper.PetOnlineAuditLogMapper;
import cn.demon.pet.mapper.PetTypeMapper;
import cn.demon.pet.service.IPetService;
import cn.demon.basic.service.impl.BaseServiceImpl;
import cn.demon.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.xml.soap.Detail;
import java.util.*;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;

    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    @Transactional
    @Override
    public void add(Pet pet) {
        super.add(pet);

        PetDetail petDetail = pet.getPetDetail();

        if (petDetail != null) {
            petDetail.setPetId(pet.getId());
            petDetailMapper.save(petDetail);
        }

    }

    @Transactional
    @Override
    public void del(Long id) {
        super.del(id);

        petDetailMapper.delPetDetailByPetId(id);
    }

    @Transactional
    @Override
    public void update(Pet pet) {
        super.update(pet);
        PetDetail petDetail = pet.getPetDetail();

        if (petDetail != null) {
            petDetail.setPetId(pet.getId());
            petDetailMapper.updatePetDetailByPetId(petDetail);
        }

    }


    /**
     * 上架实现
     * @param ids
     * @param request
     */
    @Override
    public void onsale(Long[] ids, HttpServletRequest request) {

        // 获取登录对象
        Logininfo logininfo = LoginContext.getLoginUser(request);

        // 审核
        // 获取审核人
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        for (Long id : ids) {
            Pet pet = petMapper.loadById(id);
            Boolean imageAudit = baiduAiAuditService.imageAudit(pet.getResources());
            Boolean textAudit = baiduAiAuditService.textAudit(pet.getName());
            System.out.println(imageAudit);
            System.out.println(textAudit);

            if (imageAudit && textAudit) {
                // 审核通过（修改状态 - 保存日志）
                Date date = new Date();
                pet.setOnsaletime(date);
                pet.setState(1);
                petMapper.update(pet);

                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setAuditId(pet.getId());
                log.setState(1);
                log.setAuditId(employee.getId());
                log.setAuditTime(new Date());
                log.setNote("恭喜皇上，贺喜皇上，审核通过！");
                petOnlineAuditLogMapper.save(log);

            }else{
                // 审核不通过 - 保存审核日志
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setAuditId(pet.getId());
                log.setState(-1);
                log.setAuditId(employee.getId());
                log.setAuditTime(new Date());
                log.setNote("您上传的内容让臣妾惶恐！恕臣妾做不到");
                petOnlineAuditLogMapper.save(log);
            }
        }

    }
    /**
     * 下架实现
     * @param ids
     */
    @Override
    public void offsale(Long[] ids) {
        Date offsaletime = new Date();
        petMapper.offsale(ids, offsaletime);

    }

}
